Socket
Socket
Sign inDemoInstall

edge-lexer

Package Overview
Dependencies
Maintainers
1
Versions
62
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

edge-lexer

Parses raw markup files to converts them to Tokens


Version published
Weekly downloads
14K
decreased by-12.79%
Maintainers
1
Weekly downloads
 
Created
Source

Edge lexer

Generating high level tokens from Edge whitelisted markup

travis-image appveyor-image coveralls-image npm-image

Edge lexer produces a list of tokens by scanning for Edge whitelisted syntax.

This module is a blend of a lexer and an AST generator, since Edge doesn't need a pure lexer that scans for each character. Edge markup is written within other markup languages like HTML or Markdown and walking over each character is waste of resources.

Instead, this module starts with some REGEX patterns to detect the Edge whitelisted syntax and then starts the lexical analysis within the detected markup.


Performance

Following measures are taken to keep the analysis performant

  1. Only analyse markup that is detected as Edge whitelisted syntax.
  2. Only analyse tags, that are passed to the tokenizer. Which means even if the syntax for tags is whitelisted, the tokeniser will analyse them if they are used by your app.
  3. Do not analyse Javascript expression and leave that for edge-parser.

Usage

import { Tokenizer } from 'edge-lexer'

const template = `Hello {{ username }}`
const tags = {
  if: {
    block: true,
    selfclosed: false,
    seekable: true
  }
}

// Filename is required to add it to error messages
const options = {
  filename: 'welcome.edge'
}

const tokenizer = new Tokenizer(template, tags, options)

tokenizer.parse()
console.log(tokenizer.tokens)

Features

  1. Allows multiline expressions.
  2. Whitespaces and newlines are retained.
  3. Detects for unclosed tags.
  4. Detects for unwrapped expressions and raises appropriate errors.

Terms used

This guide makes use of the following terms to identify core pieces of the tokenizer.

TermNode TypeDescription
TagblockTags are used to define logical blocks in the template engine. For example if tag or include tag.
MustachemustacheJavascript expression wrapped in curly braces. {{ }}
RawrawA raw string, which has no meaning for the template engine
NewLinenewlineNewline
CommentcommentEdge specific comment block. This will be ripped off in the output.

Nodes

Following is the list of Nodes returned by the tokenizer.

Block Node
{
  type: 'block'
  lineno: number,
  properties: BlockProp,
  children: []
}
Raw Node
{
  type: 'raw',
  lineno: number,
  value: string
}
Comment Node
{
  type: 'comment',
  lineno: number,
  value: string
}
Mustache Node
{
  type: 'mustache',
  lineno: number,
  properties: Prop
}
NewLine Node
{
  type: 'newline',
  lineno: number
}
KeyValueDescription
typestringThe type of node determines the behavior of node
linenonumberThe lineno in the source file
propertiesPropMeta data for the node. See Properties to more info.
valuestringIf node is a raw node, then value is the string in the source file
childrenarrayArray of recursive nodes. Only exists, when type === 'block'.

Properties

The properties Prop is used to define meta data for a given Node. Nodes like raw, comment and newline, doesn't need any metadata.

BlockProp

The block prop is used by the Block node. The only difference from the regular Prop is the addition of selfclosed attribute.

{
  name: string
  jsArg: string,
  raw: string,
  selfclosed: boolean
}
Prop
{
  name: string
  jsArg: string,
  raw: string
}
KeyDescription
nameThe name is the subtype for a given node. For example: if will be the name of the @if tag.
jsArgThe jsArg is the Javascript expression to evaluate
rawThe raw representation of a given expression. Used for debugging purposes.
selfclosedWhether or not the tag was selfclosed during usage.

Mustache expressions

For mustache nodes props, the name is the type of mustache expressions. The lexer supports 4 mustache expressions.

mustache

{{ username }}

e__mustache (Escaped mustache)

The following expression is ignored by edge. Helpful when you want this expression to be parsed by a frontend template engine

@{{ username }}

s__mustache (Safe mustache)

The following expression output is considered HTML safe.

{{{ '<p> Hello world </p>' }}}

es__mustache (Escaped safe mustache)

@{{{ '<p> Not touched </p>' }}}

Errors

Errors raised by the lexer are always an instance of edge-error and will contain following properties.

error.message
error.line
error.col
error.filename
error.code

Example

@if(username)
  <h2> Hello {{ username }} </h2>
@endif

The output of the above text will be

[
  {
    "type": "block",
    "properties": {
      "name": "if",
      "jsArg": "username",
      "raw": "if(username)",
      "selfclosed": false
    },
    "lineno": 1,
    "children": [
      {
        "type": "raw",
        "value": "<h2> Hello ",
        "lineno": 2
      },
      {
        "type": "mustache",
        "lineno": 2,
        "properties": {
          "name": "mustache",
          "jsArg": " username ",
          "raw": "<h2> Hello {{ username }} </h2>"
        }
      },
      {
        "type": "raw",
        "value": " </h2>",
        "lineno": 2
      },
      {
        "type": "newline",
        "lineno": 2
      }
    ]
  }
]

Change log

The change log can be found in the CHANGELOG.md file.

Contributing

Everyone is welcome to contribute. Please go through the following guides, before getting started.

  1. Contributing
  2. Code of conduct

Authors & License

thetutlage and contributors.

MIT License, see the included MIT file.

Keywords

FAQs

Package last updated on 03 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc